package net.jiug.common.config;

import com.alibaba.fastjson.JSONArray;
import net.jiug.email.model.Email;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

/**
 * 邮箱配置
 */
public class ConfigEmail {

    public static String nick ;
    // 默认通知标题
    public static String noticeTitle ;
    // 邮箱使用阈值
    public static Integer emailUseThresholdValue ;

    // 邮箱列表
    public static List<Email> emailList ;

    // 密钥
    public static Set<String> secretKey ;

    static {
        try {
            // 加载配置文件
            InputStream inputStream = ConfigEmail.class.getClassLoader().getResourceAsStream("configEmail.properties");
            InputStreamReader isr = new InputStreamReader(inputStream, "UTF-8");
            Properties props = new Properties();
            props.load(isr);

            // 设置单一变量值
            nick = props.getProperty("nick");
            noticeTitle = props.getProperty("noticeTitle");
            emailUseThresholdValue = Integer.parseInt(props.getProperty("emailUseThresholdValue"));
            // 赋值JSON对象数组到list
            String emailJsonArray = props.getProperty("emailJsonArray");
            emailList = JSONArray.parseArray(emailJsonArray,Email.class);
            // 赋值数组到set
            String[] secretKeyArray = props.getProperty("secretKey").split(",");
            secretKey = new HashSet<>();
            for (String item:secretKeyArray) {
                secretKey.add(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
